package com.yanqu.road.dao.impl.activity.recallplayer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.recallplayer.data.RecallPlayerBeBindTaskValue;
import com.yanqu.road.entity.activity.recallplayer.data.UserRecallBeBindData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserRecallBeBindDataDaoImpl extends TempDataObjectDao<UserRecallBeBindData> {
    public static String getUserRecallBeBindDataSql = "select * from `t_u_recalled_bind_data` where `userId`=?";

    public UserRecallBeBindData getUserRecallBeBindData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserRecallBeBindDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserRecallBeBindDataSql = "update `t_u_recalled_bind_data` set recallCode=?,userType=?,taskInfos=? where userId=?";

    @Override
    protected boolean updateData(UserRecallBeBindData userRecallBeBindData) {
        userRecallBeBindData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, userRecallBeBindData.getRecallCode()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallBeBindData.getUserType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallBeBindData.getTaskInfos())));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallBeBindData.getUserId()));
        if (getDb().executeNoneQuery(updateUserRecallBeBindDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserRecallBeBindDataSql = "insert into `t_u_recalled_bind_data`(userId,recallCode,userType,taskInfos) values (?,?,?,?)";

    @Override
    protected boolean addData(UserRecallBeBindData userRecallBeBindData) {
        userRecallBeBindData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRecallBeBindData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRecallBeBindData.getRecallCode()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallBeBindData.getUserType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallBeBindData.getTaskInfos())));
        if (getDb().executeNoneQuery(insertUserRecallBeBindDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserRecallBeBindData parseData(ResultSet rs) throws SQLException {
        UserRecallBeBindData userRecallBeBindData = new UserRecallBeBindData();
        userRecallBeBindData.setUserId(rs.getLong("userId"));
        userRecallBeBindData.setRecallCode(rs.getString("recallCode"));
        userRecallBeBindData.setUserType(rs.getInt("userType"));
        if (!StringUtils.isNullOrEmpty(rs.getString("taskInfos"))) {
            userRecallBeBindData.setTaskInfos(JSON.parseObject(rs.getString("taskInfos"), new TypeReference<List<RecallPlayerBeBindTaskValue>>() {
            }));
        }

        return userRecallBeBindData;
    }
}
